package hotciv.common;

import java.util.Map;
import java.util.HashMap;

import hotciv.framework.*;

public class WorldCreator {
	
	/**
	 * Creates a map with TileImpl's from a String array
	 * @param layout - the layout array
	 * @return the map with TileImpl's
	 */
	public static Map<Position, TileImpl> createWorldFromStringArray(String[] layout) {
		return createWorldFromStringArray(layout, false);
	}

	/**
	 * Creates a map with TileImpl's from a String array
	 * @param layout - the layout array
	 * @param noPlains - if this is set to true, then do not create plains
	 * @return the map with TileImpl's
	 */
	public static Map<Position, TileImpl> createWorldFromStringArray(String[] layout, boolean noPlains) {
		Map <Position, TileImpl> world = new HashMap<Position, TileImpl>();
		String line;
		for ( int r = 0; r < GameConstants.WORLDSIZE; r++ ) {
			line = layout[r];
			for ( int c = 0; c < GameConstants.WORLDSIZE; c++ ) {
				char tileChar = line.charAt(c);
				String type = "error";
				if ( tileChar == '.' ) { type = GameConstants.OCEANS; }
				if ( tileChar == 'o' ) { type = GameConstants.PLAINS; }
				if ( tileChar == 'M' ) { type = GameConstants.MOUNTAINS; }
				if ( tileChar == 'f' ) { type = GameConstants.FOREST; }
				if ( tileChar == 'h' ) { type = GameConstants.HILLS; }
				Position p = new Position(r,c);
				if (!noPlains || type != GameConstants.PLAINS) {
					world.put( p, new TileImpl(p, type));
				}
			}
		}
		
		return world;
	}
}
